float4x4 MAT_WORLD_VIEW_PROJ;
float4x4 MAT_WORLD_VIEW;
float4x4 MAT_WORLD;
float4x4 MAT_PROJ_TEXTURE;

float4 ambientLight = float4(0.4f, 0.4f, 0.4f, 1.0f);
float4 diffuseLight = float4(0.3f, 0.3f, 0.3f, 0.4);
float4 specularLight = float4(0.2f, 0.2f, 0.2f, 1.0f);

float3 lightDirection = float3(0.577f, 0.577f, 0.577f);

float shininess = 30.0f;

float fogStart = 1500.0f;
float fogEnd = 4000.0f;
float4 fogColor = float4(0.6f, 0.6f, 0.6f, 1.0f);

float4 waterColor = float4(0.6f, 0.6f, 0.6f, 1.0f);

float fresnelPower = 6.0f;
float fresnelScale = 2.0f;
float fresnelBias = 0.0f;

float3 eyePos;

texture texReflection = NULL;
texture texRefraction = NULL;
texture texWaterBump = NULL;

float bumpFactor = 0;

sampler SamplerTexReflection = sampler_state
{
	Texture = <texReflection>;

	MipFilter = Linear;
	MinFilter = Linear;
	MagFilter = Linear;

	AddressU = Clamp;
	AddressV = Clamp;
};

sampler SamplerTexRefraction = sampler_state
{
	Texture = <texRefraction>;
	MipFilter = Linear;
	MinFilter = Linear;
	MagFilter = Linear;

	AddressU = Wrap;
	AddressV = Wrap;
};

sampler SamplerTexWaterBump = sampler_state
{
	Texture = <texWaterBump>;

	MipFilter = Linear;
	MinFilter = Linear;
	MagFilter = Linear;

	AddressU = Wrap;
	AddressV = Wrap;
};

void WaterNormalVS(
									 float3 iPos : POSITION,

									 out float4 oPos : POSITION,
									 out float4 oPosT : TEXCOORD0,
									 out float3 oPosWorld : TEXCOORD1,
									 out float3 oLookDir : TEXCOORD2,
									 out float oFog : TEXCOORD3
									 )
{
	float4 pos = float4(iPos, 1);
	oPos = mul(pos, MAT_WORLD_VIEW_PROJ);    
	oPosT = oPos;
	oPosWorld = mul(pos, MAT_WORLD);
	oLookDir = normalize(eyePos - oPosWorld);
	float4 posView = mul(pos, MAT_WORLD_VIEW);
	oFog = (posView.z - fogStart) / (fogEnd - fogStart);
}
/*
float4 WaterNormalPS(
float4 iPosT : TEXCOORD0,
float3 iPosWorld : TEXCOORD1,
float3 iLookDir : TEXCOORD2,
float iFog : TEXCOORD3
) : COLOR0
{
// sample bump.
float2 pos = float2(iPosWorld.xz * 0.005f + bumpFactor * 0.1);
float3 bump = tex2D(SamplerTexWaterBump, pos);
bump = 2 * bump - 1;
bump.xy *= 0.15;
bump.z = 0.8 * abs(bump.z) + 0.2;

// offset the projected texture coord.
float dis = distance(iPosWorld, eyePos);
float factor = clamp(dis / 1600.0f, 0, 1);

// sample the reflection.
float4 proj = iPosT / iPosT.w;
float2 projCoord = 0.5 - proj * 0.5;
projCoord.x -= bump.x * 0.4f * (1 - factor);    
float4 refl = tex2D(SamplerTexReflection, projCoord);    

// sample the refraction.
projCoord.x = 0.5 + proj.x * 0.5;
projCoord.y = 0.5 - proj.y * 0.5;
projCoord -= bump * 0.4 * (1 - factor);
float4 refr = tex2D(SamplerTexRefraction, projCoord);

// calculate the normal.
float3 normal = normalize(float3(0, 1, 0));// + bump.yzx * 5);    

// lerp the reflection and the refraction.
float lrp = 1 - dot(iLookDir, normal);
float fresnel = saturate(fresnelBias + fresnelScale * pow(abs(lrp), fresnelPower));

// Interpolate between the refraction and reflection
float4 color = lerp(refr, refl, fresnel);

// calculate diffuse light.
float4 diffuse = diffuseLight * max(0, dot(normal, lightDirection));

// calculate specular light.
float3 H = normalize(iLookDir + lightDirection);
float4 specular = pow(max(0.000001f, dot(H, normal)), shinness) * specularLight;

color =  color * waterColor + diffuse + specular;
return lerp(color, fogColor, iFog);
}
*/

float4 WaterNormalPS(
										 float4 iPosT : TEXCOORD0,
										 float3 iPosWorld : TEXCOORD1,
										 float3 iLookDir : TEXCOORD2,
										 float iFog : TEXCOORD3
										 ) : COLOR0
{
	// sample bump.
	float2 pos = float2(iPosWorld.xz * 0.005f + bumpFactor * 0.1);
	float3 bump = tex2D(SamplerTexWaterBump, pos);
	bump = 2 * bump - 1;
	bump.xy *= 0.15;
	bump.z = 0.8 * abs(bump.z) + 0.2;

	// offset the projected texture coord.
	float dis = distance(iPosWorld, eyePos);
	float factor = clamp(dis / 1600.0f, 0, 1);

	// sample the reflection.
	float4 proj = iPosT / iPosT.w;
	float2 projCoord;

	// sample the refraction.
	projCoord.x = 0.5 + proj.x * 0.5;
	projCoord.y = 0.5 - proj.y * 0.5;
	projCoord -= bump * 0.4 * (1 - factor);
	float4 refr = tex2D(SamplerTexRefraction, projCoord);

	// Interpolate between the refraction and reflection
	float4 color = refr;

	float3 normal = normalize(float3(0, 1, 0) + bump.yzx);    
	//float3 normal = normalize(bump.yzx);

	// calculate diffuse light.
	float4 diffuse = diffuseLight * max(0, dot(normal, lightDirection));

	// calculate specular light.
	float3 H = normalize(iLookDir + lightDirection);
	float4 specular = pow(max(0.000001f, dot(H, normal)), shininess) * specularLight;

	color =  color * waterColor + specular;

	//return lerp(color, fogColor, iFog);
	return color;
}
//////////////////////////////////////////
// technique list.
technique waterNormal
{
	pass P0
	{
		//CullMode = CCW;
		CullMode = None;
		ZEnable = True;
		ZWriteEnable = True;
		AlphaBlendEnable = False;
		SrcBlend = SrcAlpha;
		DestBlend = InvSrcAlpha;
		AlphaTestEnable = True;
		AlphaRef = 254;
		ALPHAFUNC = GREATER;
		VertexShader = compile vs_2_0 WaterNormalVS();
		PixelShader  = compile ps_2_0 WaterNormalPS();
	}
}


